﻿using MediatR;
using ProductIntory.Domains;
using ProductIntory.ErrorCodes;
using ProductIntory.Infrastructures.Interface;
using ProductIntory.Read.Apis.Application.Command.ProductionStorageCommands;

namespace ProductIntory.Read.Apis.Application.CommandHandler.ProductionStorageCommandHandlers
{
    public class ShowProductionStorageCommandHandler : IRequestHandler<ShowProductionStorageCommand,ApiPaging<List<ProductionStorage>>>
    {
        IBaseRepository<ProductionStorage> ProductionStorage;

        public ShowProductionStorageCommandHandler(IBaseRepository<ProductionStorage> ProductionStorage)
        {
            this.ProductionStorage = ProductionStorage;
        }

        public async Task<ApiPaging<List<ProductionStorage>>> Handle(ShowProductionStorageCommand request, CancellationToken cancellationToken)
        {
            var list = ProductionStorage.GetAll();
            var totalCount = list.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / request.pagesize);
            var data = list.OrderBy(x => x.Productionstorage_ID).Skip((request.currentpage - 1) * request.pagesize).Take(request.pagesize).ToList();
            return new ApiPaging<List<ProductionStorage>>
            {
                currentpage = request.currentpage,
                totalcount = totalCount,
                totalpages = totalPages,
                pagesize = request.pagesize,
                Code = ApiEnum.success,
                Message = "分页成功",
                Data =data
            };
        }
    }
}
